package com.jcj.jcjsharding.demo;

import com.jcj.jcjsharding.config.ShardingAutoConfiguration;
import com.jcj.jcjsharding.mybatis.ShardingMapperFactoryBean;
import com.jcj.jcjsharding.demo.mapper.UserMapper;
import com.jcj.jcjsharding.demo.model.User;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;

@Slf4j
@SpringBootApplication
@Import({ShardingAutoConfiguration.class})
@MapperScan(value = "com.jcj.jcjsharding.demo.mapper", factoryBean = ShardingMapperFactoryBean.class)
public class JcjShardingApplication {

    public static void main(String[] args) {
        SpringApplication.run(JcjShardingApplication.class, args);
    }

    @Autowired
    private UserMapper userMapper;

    @Bean
    ApplicationRunner applicationRunner() {
        return d -> {

            for (int i = 1; i < 10; i++) {
                User user = userMapper.findById(i);
                if (user == null) {
                    log.info("insert user");
                    int result = userMapper.insert(new User(i, "jcj", 18));
                    log.info("insert 结果：" + result);
                }
                User user2 = userMapper.findById(i);
                log.info("find user");
                log.info("user:{}", user2);

                log.info("update user");
                int result = userMapper.update(new User(i, "jcj", 19));
                log.info("update 结果：" + result);
                User user3 = userMapper.findById(i);
                log.info("update user:{}", user3);

                log.info("delete user");
                int result2 = userMapper.delete(i);
                log.info("delete 结果：" + result2);
            }
        };
    }

}
